#include "QLivebroadcastroom.h"
#include <QImage>


QLiveBroadcastRoom::QLiveBroadcastRoom(QWidget *parent) : QWidget(parent)
{
    LabelOfView = new QLabel(this);

    WidgetOfBtns = new QWidget(this);
    BtnOfStartlive = new QPushButton(WidgetOfBtns);
    BtnOfSettings = new QPushButton(WidgetOfBtns);

    WidgetOfWindowsTitleBar = new QWidget(this);
    CloseBtn = new QPushButton(WidgetOfWindowsTitleBar);
    MaximizeBtn = new QPushButton(WidgetOfWindowsTitleBar);
    MinimizeBtn = new QPushButton(WidgetOfWindowsTitleBar);

    LayoutOfAll = new QVBoxLayout;
    LayoutOfUp = new QHBoxLayout;
    LayoutOfDown = new QHBoxLayout;

    CloseBtn->setFixedSize(45,45);
    MaximizeBtn->setFixedSize(45,45);
    MinimizeBtn->setFixedSize(45,45);

    WidgetOfWindowsTitleBar->setFixedHeight(60);
    LayoutOfUp->addStretch();
    LayoutOfUp->addWidget(MinimizeBtn);
    LayoutOfUp->addWidget(MaximizeBtn);
    LayoutOfUp->addWidget(CloseBtn);
    WidgetOfWindowsTitleBar->setLayout(LayoutOfUp);


//    BtnOfSettings->setFixedSize(45,45);
//    BtnOfStartlive->setFixedSize(45,45);

    WidgetOfBtns->setFixedHeight(60);
    LayoutOfDown->addStretch();
    LayoutOfDown->addWidget(BtnOfSettings);
    LayoutOfDown->addWidget(BtnOfStartlive);
    WidgetOfBtns->setLayout(LayoutOfDown);

    this->setMinimumSize(1156,680);
    this->setWindowIcon(QIcon(":/ALL/Image/ALL/Cat2.png"));
    this->setWindowFlags(Qt::FramelessWindowHint);
    mIsMax = false;//当前并未最大化
    mDrag = false;//当前并未在拖动


    LayoutOfAll->setMargin(0);
    LayoutOfAll->setSpacing(0);
    LayoutOfAll->addWidget(WidgetOfWindowsTitleBar);
    LayoutOfAll->addWidget(LabelOfView);
    LayoutOfAll->addWidget(WidgetOfBtns);
    WidgetOfWindowsTitleBar->installEventFilter(this);
    this->setLayout(LayoutOfAll);


    CloseBtn->setObjectName("CloseBtn");
    MinimizeBtn->setObjectName("MinimizeBtn");
    MaximizeBtn->setObjectName("MaximizeBtn");

    MaximizeBtn->setCheckable(true);
    connect(CloseBtn,SIGNAL(clicked()),this,SLOT(close()));
    connect(MinimizeBtn,&QPushButton::clicked,this,&QWidget::showMinimized);
    connect(MaximizeBtn,SIGNAL(clicked()),this,SLOT(SLOTFunOfMaximize()));

    QReadQSS::setStyle(":/QSS/QSS/Rester.qss");
}

void QLiveBroadcastRoom::SLOTFunOfMaximize()
{
//    qDebug()<<mIsMax;
    if(!mIsMax)
    {
//        qDebug()<<"当前未最大化";
        this->showMaximized();
        MaximizeBtn->setObjectName("ReductionBtn");
        QReadQSS::setStyle(":/QSS/QSS/Rester.qss");
        mIsMax = true;
    }
    else
    {
//        qDebug()<<"当前已经最大化";
        this->showNormal();
        MaximizeBtn->setObjectName("MaximizeBtn");
        QReadQSS::setStyle(":/QSS/QSS/Rester.qss");
        mIsMax = false;
    }
}

bool QLiveBroadcastRoom::eventFilter(QObject *obj, QEvent *event)
{
    if (obj == WidgetOfWindowsTitleBar)
    {
        if (event->type() == QEvent::MouseButtonDblClick)
        {
            SLOTFunOfMaximize();
            return true;
        }
    }
    return QObject::eventFilter(obj,event);
}

void QLiveBroadcastRoom::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton && event->y() <= 60)
    {
        mDrag = true;
        mDragPos = event->globalPos() - pos();
        event->accept();
    }
}

void QLiveBroadcastRoom::mouseMoveEvent(QMouseEvent *event)
{
    if (mDrag && (event->buttons() && Qt::LeftButton) && event->y() <= 60)
    {
        this->showNormal();
        mIsMax = false;
        move(event->globalPos() - mDragPos);
        event->accept();
    }
}

void QLiveBroadcastRoom::mouseReleaseEvent(QMouseEvent *event)
{
    mDrag = false;
}
